package com.wengzw.controller;

import com.wengzw.po.User;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@Api(value = "",tags = "测试")
public class UserController {
    @RequestMapping(value="/hello")
    @ResponseBody
    public User hello( User user) {
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        return user;
    }

    /**
     * 向用户注册页面跳转
     */
    @RequestMapping("/toRegister")
    public String toRegister() {
        return "register";
    }

    /**
     * 接收用户注册信息
     */
    @RequestMapping("/registerUser")
    public String registerUser(User user) {
        String username = user.getUsername();
        Integer password = user.getPassword();
        System.out.println("username=" + username);
        System.out.println("password=" + password);
        return "success";
    }

    @RequestMapping("/deleteUsers")
    public String deleteUsers(Integer[] ids) {
        System.out.print("aaaa");
        if (ids != null) {
            for (Integer id : ids) {
                // 使用输出语句模拟已经删除了用户
                System.out.println("删除了id为" + id + "的用户！");
            }
        } else {
            System.out.println("ids=null");
        }
        return "success";
    }


    @RequestMapping(value = "/tologin")
    public String login() {
        return "login";
    }
    //用户登陆
    @RequestMapping(value = "/login")
    public String logintomain(User user, Model model, HttpSession session) {
        String username = user.getUsername();
        Integer password = user.getPassword();
        if(username!=null && username.equals("wengzw") && password!=null && password.equals(123456)){
            session.setAttribute("user_session",user);
            return "redirect:main";
        }else{
            model.addAttribute("msg","用户名和密码错误");
            return "login";
        }
    }

    //向主页面跳转
    @RequestMapping(value = "/main")
    public String toMain(){
        return "main";
    }

    //退出登陆
    @RequestMapping(value = "/loginout" )
    public String tologinout(HttpSession session){
        session.invalidate();
        return "redirect:tologin";
    }
}
